<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登录处理中...</title>
    <style>
        body {
            background: #f5f5f5;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
            margin: 0;
            font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
        }
        .status-box {
            background: white;
            padding: 30px 40px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            text-align: center;
        }
        .spinner {
            width: 40px;
            height: 40px;
            border: 4px solid #f3f3f3;
            border-top: 4px solid #07c160;
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin: 20px auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .error {
            color: #e74c3c;
            margin-top: 20px;
        }
    </style>
</head>
<body>
<div class="status-box">
    <div class="spinner"></div>
    <h2>正在完成登录授权...</h2>
    <p>请稍候，即将自动关闭窗口</p>
    <div class="error" id="errorMsg" style="display: none;"></div>
</div>

<script th:inline="javascript">
    window.onload = function() {
        const errorMsg = /*[[${errorMsg}]]*/ null;
        const userInfo = {
            openid: /*[[${userInfo?.openid}]]*/ null,
            nickname: /*[[${userInfo?.nickname}]]*/ null,
            headimgurl: /*[[${userInfo?.headimgurl}]]*/ null,
            error: errorMsg
        };

        // 动态检测合法来源
        const currentOrigin = window.location.origin;
        const allowedOrigins = [
            currentOrigin,  // 当前域名
            'http://localhost:8080'  // 开发环境
        ];

        try {
            if (errorMsg) {
                throw new Error(errorMsg);
            }

            if (!window.opener) {
                throw new Error('请勿直接访问此页面！');
            }

            // 查找匹配的合法源
            const targetOrigin = allowedOrigins.find(origin => {
                try {
                    return new URL(origin).hostname === location.hostname;
                } catch(e) {
                    return false;
                }
            });

            if (!targetOrigin) {
                throw new Error('非法的访问来源');
            }

            // 发送消息到父窗口
            window.opener.postMessage(
                JSON.stringify(userInfo),
                targetOrigin
            );

            // 延迟关闭确保消息发送
            setTimeout(() => {
                window.close();
            }, 300);

        } catch (error) {
            document.getElementById('errorMsg').textContent = error.message;
            document.getElementById('errorMsg').style.display = 'block';
            document.querySelector('.spinner').style.display = 'none';
            document.querySelector('h2').textContent = '授权失败';
            document.querySelector('p').style.display = 'none';
        }
    };
</script>
</body>
</html>